﻿using MediatR;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.COA
{
    public class AddCOAIPQCDataHandler : IRequestHandler<AddCOAIPQCDataCommand, Result>
    {
        public AddCOAIPQCDataHandler(IBaseRepository<COAData> cOADataRepository, IBaseRepository<COAStandardData> cOAStandardDataRepository)
        {
            COADataRepository = cOADataRepository;
            COAStandardDataRepository = cOAStandardDataRepository;
        }

        public IBaseRepository<COAData> COADataRepository { get; set; }
        public IBaseRepository<COAStandardData> COAStandardDataRepository { get; set; }
        public async Task<Result> Handle(AddCOAIPQCDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            COAData cOAData = new COAData()
            {
                COAId = request.COAId,
                LiveAnimalsDocument = request.LiveAnimalsDocument,
                LiveAnimalsDocumentTime = request.LiveAnimalsDocumentTime,
                COARType = request.COARType,
                COAGoods = request.COAGoods,
                CheckInBatch = request.CheckInBatch,
                COAQuarantineType = request.COAQuarantineType,
                OriginAddr = request.OriginAddr,
                SampleSize = request.SampleSize,
                SampleNum = request.SampleNum,
                SampleTime = request.SampleTime,
                SampleData = request.SampleData,
                COATime = request.COATime,
                ExecuteData = request.ExecuteData,
                COAAuditState = request.COAAuditState,
                COACreateName = request.COACreateName,
                COACreateTime = request.COACreateTime,
                ISDeleteCOA = request.ISDeleteCOA,
                COAImage = request.COAImage,
                ProductionName = request.ProductionName,
                COAverdict = request.COAverdict
            };
            var AddNum = COADataRepository.Add(cOAData);
            int AddGoodsNum = 0;
            if (AddNum > 0)
            {
                for (int i = 0; i < request.COAStandardContext.Count; i++)
                {
                    COAStandardData cOAStandardData = new COAStandardData()
                    {
                        COAStandardId = 0,
                        COAId = cOAData.COAId,
                        IPQCId = Convert.ToInt32(request.IPQCId[i]),
                        COAStandardContext = request.COAStandardContext[i].ToString(),
                        COAStandardVerdict = request.COAStandardVerdict[i].ToString(),
                    };
                    AddGoodsNum = COAStandardDataRepository.Add(cOAStandardData);
                }
                if (AddGoodsNum > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
    }
}
